package com.jk.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.jk.service.AlipayService;
import com.jk.util.AlipayConfig;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.UUID;

/**
 * 类描述：
 *
 * @ClassName AlipayServiceImpl
 * @Description TODO
 * @Author fandasen
 * @Date 2022/6/7 18:00
 * @Version 1.0
 */
@Service("alipayService")
public class AlipayServiceImpl implements AlipayService {

    /** 调取支付宝接口 web端支付*/
    DefaultAlipayClient alipayClient = new DefaultAlipayClient(
            AlipayConfig.gatewayUrl, AlipayConfig.APP_ID, AlipayConfig.APP_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.sign_type);
    /** 调取支付宝接口app端支付*/
    AlipayClient alipayClients = new DefaultAlipayClient(
            AlipayConfig.gatewayUrl, AlipayConfig.APP_ID, AlipayConfig.APP_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.sign_type);
    //    支付
    @Override
    public String pay(String order_number,String total_amount,HttpServletResponse response, HttpServletRequest request) throws IOException, AlipayApiException {
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        /** 同步通知，支付完成后，支付成功页面*/
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        /** 异步通知，支付完成后，需要进行的异步处理*/
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
//        //商户订单号，后台可以写一个工具类生成一个订单号，必填
//        String order_number = new String("111");
//        //付款金额，从前台获取，必填
//        String total_amount = new String("111");
        //订单名称/标题，可自定义
        String subject = new String("支付宝沙箱测试");
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + order_number + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                +"\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String result = alipayClient.pageExecute(alipayRequest).getBody().replace('\"','\'').replace('\n',' ');
        return result;
    }

    @Override
    public String refund1(HttpServletResponse response, HttpSession session,String out_trade_no,String refund_amount) throws IOException, AlipayApiException {
        //设置请求参数
        AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();
        //商户订单号，必填
//        String out_trade_no = new String("12345");
//        //需要退款的金额，该金额不能大于订单金额，必填
//        String refund_amount = new String("1000");
        //标识一次退款请求，同一笔交易多次退款需要保证唯一。如需部分退款，则此参数必传；不传该参数则代表全额退款
        String out_request_no = new String(UUID.randomUUID().toString());
        // 字符转义很重要
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"refund_amount\":\""+ refund_amount +"\","
                + "\"out_request_no\":\""+ out_request_no +"\"}");
        //请求
        String result = alipayClient.execute(alipayRequest).getBody();
        //输出
        System.out.println(result);//以下写自己的订单退款代码
//    logger.info("返回结果={}",result);
        return result;
    }

}
